package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 前序遍历
 */
public class LeetCode144 {
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        List<Integer> result = new ArrayList<>();
        while (!stack.empty()) {
            TreeNode mid = stack.pop();
            result.add(mid.val);
            if (mid.right!=null){
                stack.push(mid.right);
            }
            if (mid.left!=null){
                stack.push(mid.left);
            }

        }
        return result;
    }
}
